package com.qiall.project.service.addproject;

import com.qiall.assertutil.Assert;
import com.qiall.common.exception.BusinessException;
import com.qiall.common.request.BaseActivity;

import com.qiall.common.request.BaseResponse;
import com.qiall.common.request.GetByIdRequest;
import com.qiall.project.ability.request.AddProjectRequest;
import com.qiall.user.ability.entity.IsAdminDTO;
import com.qiall.user.ability.entity.OrganizationResponse;
import com.qiall.user.ability.entity.UserInfoDTO;
import com.qiall.user.ability.service.organization.OrganizationService;
import com.qiall.user.ability.service.user.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import com.qiall.common.exception.ResultCode;
import org.springframework.stereotype.Component;


/**
 * @author 吴金辉
 */
@Component
public class AddProjectCheckActivity extends BaseActivity<AddProjectRequest, BaseResponse> {
    @DubboReference
    private UserService userService;
    @DubboReference
    private OrganizationService organizationService;


    @Override
    public void handler(AddProjectRequest request, BaseResponse response) {
        notNullCheck(request);
        checkAuthority(request);
    }

    private void notNullCheck(AddProjectRequest request) {
        Assert.notNull(request.getProjectName(), "项目名称为空");
        Assert.notNull(request.getContext(), "内容为空");
        Assert.notNull(request.getPermission(), "权限为空");
        Assert.notNull(request.getStartTime(), "项目开始时间为空");
    }

    private void checkAuthority(AddProjectRequest request) {

        UserInfoDTO userInfoDTO = userService.getUserInfoByAuthCode(request).getBody();
        Assert.notNull(userInfoDTO, "用户未登录");
        request.setOrgId(userInfoDTO.getOrgId());
        request.setProjectLeaderId(userInfoDTO.getUserId());
        request.setCreatorId(userInfoDTO.getUserId());
        request.setProjectMemberIds(userInfoDTO.getUserId()+"");
    }


}
